using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using DataTypes;

namespace _395_2d
{
    public class Game1 : Microsoft.Xna.Framework.Game, ISubscriber
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private SpriteFont spriteFont;
        SpriteManager sm;

        private StartScreen ss;

        Background background;
        HUD HUD;


        Song gameMusic;
        Song failMusic;
        Song startMusic;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            ss = new StartScreen(this);
            Components.Add(ss);
            ss.Subscribe(this);
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            gameMusic = Content.Load<Song>("music");
            failMusic = Content.Load<Song>("fail_music2");
            startMusic = Content.Load<Song>("start_music");

            MediaPlayer.Play(startMusic);
            spriteFont = this.Content.Load<SpriteFont>("HUDFont");
        }

        protected override void UnloadContent()
        {
            
        }

        protected override void Update(GameTime gameTime)
        {
            
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
           

            base.Draw(gameTime);
        }

        public void Deliver(Message m)
        {
            if (m is StartGameMessage)
            {
                MediaPlayer.Play(gameMusic);
                StartGameMessage sgm = (StartGameMessage)m;
                Components.Remove(ss);

                background = new Background(this);
                Components.Add(background);

                sm = new SpriteManager(this, sgm.Time);
                Components.Add(sm);

                HUD = new HUD(this);
                Components.Add(HUD);

                sm.Subscribe(HUD);
                sm.Subscribe(this);
            }

            if (m is GameOverMessage)
            {
                MediaPlayer.Play(failMusic);
                GameOverMessage gom = (GameOverMessage)m;
                Components.Remove(background);
                Components.Remove(sm);

                int i = HUD.Score;
                Components.Remove(HUD);

                Components.Add(new GameOverScreen(this, gom, i));
            }
        }
    }
}
